import%20marimo%0A%0A__generated_with%20%3D%20%220.17.6%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20auto_download%3D%5B%22html%22%2C%20%22ipynb%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20pytz%0A%20%20%20%20import%20datetime%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20india_timezone%20%3D%20pytz.timezone(%22Asia%2FKolkata%22)%0A%20%20%20%20now%20%3D%20datetime.datetime.now(india_timezone)%0A%0A%20%20%20%20curr%20%3D%20now.strftime(%22%25Y-%25m-%25d%2C%20%25I%3A%25M%3A%25S%20%25p%20%25Z%22)%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20rf%22%22%22%0A%20%20%20%20%23%20Week%20-%205%0A%0A%20%20%20%20The%20last%20column%20of%20the%20dataset%20(Credit_Limit)%20corresponds%20to%20the%20target%20variable.%20Separate%20the%20dataset%20to%20have%20the%20feature%20matrix%20as%20X%20and%20the%20target%20variable%20as%20y.%20Use%20random_state%20%3D%2042%20wherever%20applicable.%0A%0A%0A%20%20%20%20Split%20the%20dataset%20into%20train%20and%20test%20set%20with%20a%20**70%3A30%20ratio**%20and%20%60random_state%20%3D%2042%60.%20Use%20these%20to%20answer%20all%20the%20questions%20that%20follow.%20%0A%0A%20%20%20%20**Submission%20Date%3A**%20%602025-10-31%2C%2023%3A59%20IST%60%0A%0A%20%20%20%20**Last%20Updated%3A**%20%60%7Bcurr%7D%60%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%2C%20GridSearchCV%0A%20%20%20%20from%20sklearn.metrics%20import%20r2_score%2C%20root_mean_squared_error%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%2C%20Ridge%2C%20Lasso%0A%20%20%20%20from%20sklearn.neighbors%20import%20KNeighborsRegressor%0A%20%20%20%20from%20sklearn.tree%20import%20DecisionTreeRegressor%0A%20%20%20%20from%20sklearn.ensemble%20import%20AdaBoostRegressor%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20AdaBoostRegressor%2C%0A%20%20%20%20%20%20%20%20DecisionTreeRegressor%2C%0A%20%20%20%20%20%20%20%20GridSearchCV%2C%0A%20%20%20%20%20%20%20%20KNeighborsRegressor%2C%0A%20%20%20%20%20%20%20%20Lasso%2C%0A%20%20%20%20%20%20%20%20LinearRegression%2C%0A%20%20%20%20%20%20%20%20Ridge%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20r2_score%2C%0A%20%20%20%20%20%20%20%20root_mean_squared_error%2C%0A%20%20%20%20%20%20%20%20train_test_split%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(pd)%3A%0A%20%20%20%20df%20%3D%20pd.read_csv(%22Week-5%2FGraded%20Assignment%2Fdataset.csv%22)%0A%20%20%20%20return%20(df%2C)%0A%0A%0A%40app.cell%0Adef%20_(df)%3A%0A%20%20%20%20df.sample(5)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df)%3A%0A%20%20%20%20df.info()%0A%20%20%20%20df.describe().T%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df)%3A%0A%20%20%20%20_cols%20%3D%20df.select_dtypes(include%3D%22float%22).columns.tolist()%0A%20%20%20%20for%20col%20in%20_cols%3A%0A%20%20%20%20%20%20%20%20print(f%22%7Bcol%7D%3A%20%7Blen(df%5Bcol%5D.unique())%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df)%3A%0A%20%20%20%20X%20%3D%20df.drop(%22Credit_Limit%22%2C%20axis%3D1)%0A%20%20%20%20y%20%3D%20df%5B%22Credit_Limit%22%5D%0A%20%20%20%20return%20X%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(X%2C%20train_test_split%2C%20y)%3A%0A%20%20%20%20X_train%2C%20X_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20test_size%3D0.3%2C%20random_state%3D42%0A%20%20%20%20)%0A%0A%20%20%20%20X_train.shape%2C%20X_test.shape%2C%20y_train.shape%2C%20y_test.shape%0A%20%20%20%20return%20X_test%2C%20X_train%2C%20y_test%2C%20y_train%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%201%20-%202%0A%0A%20%20%20%20Train%20a%20Linear%20Regression%20model%20on%20the%20training%20dataset%20with%20fit_intercept%20%3D%20False%20and%20compute%20%E2%80%98r2_score%E2%80%99%20on%20the%20test%20dataset%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression)%3A%0A%20%20%20%20lr_model%20%3D%20LinearRegression(fit_intercept%3DFalse)%0A%20%20%20%20return%20(lr_model%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20lr_model%2C%20y_train)%3A%0A%20%20%20%20lr_model.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%201%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20r2_score%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20lr_model%2C%20np%2C%20r2_score%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20lr_model.predict(X_test)%0A%0A%20%20%20%20np.round(r2_score(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%202%0A%0A%20%20%20%20What%20is%20the%20index%20of%20the%20feature%20with%20the%20highest%20absolute%20coefficient%20value%3F%0A%0A%20%20%20%20**Note%3A**%20that%20index%20starts%20from%200%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(lr_model)%3A%0A%20%20%20%20lr_model.coef_.argmax()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%203%20-%204%0A%0A%20%20%20%20Train%20a%20Ridge%20model%20on%20the%20training%20data%20with%20the%20following%20parameters%3A%0A%0A%20%20%20%20-%20solver%20%3D%20%E2%80%98sag%E2%80%99%0A%20%20%20%20-%20tol%20%3D%200.0005%0A%20%20%20%20-%20random_state%20%3D%2042%0A%0A%20%20%20%20Compute%20the%20%E2%80%98**r2_score**%E2%80%99%20on%20the%20test%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Ridge)%3A%0A%20%20%20%20rd_model%20%3D%20Ridge(solver%3D%22sag%22%2C%20tol%3D0.0005%2C%20random_state%3D42)%0A%20%20%20%20return%20(rd_model%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20rd_model%2C%20y_train)%3A%0A%20%20%20%20rd_model.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%203%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20**r2_score**%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20np%2C%20r2_score%2C%20rd_model%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20rd_model.predict(X_test)%0A%0A%20%20%20%20np.round(r2_score(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%204%0A%0A%20%20%20%20Enter%20the%20value%20of%20the%20%60intercept_%60%20attribute%20obtained%20after%20training%20the%20model.%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20rd_model)%3A%0A%20%20%20%20np.round(rd_model.intercept_%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%205%20-%206%0A%0A%20%20%20%20Train%20a%20Lasso%20model%20on%20the%20training%20data%20with%20the%20following%20parameters%3A%0A%0A%20%20%20%20-%20alpha%20%3D%20100%0A%20%20%20%20-%20random_state%20%3D%2042%0A%0A%20%20%20%20Compute%20the%20%E2%80%98**r2_score**%E2%80%99%20on%20the%20test%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Lasso)%3A%0A%20%20%20%20ls_model%20%3D%20Lasso(alpha%3D100%2C%20random_state%3D42)%0A%20%20%20%20return%20(ls_model%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20ls_model%2C%20y_train)%3A%0A%20%20%20%20ls_model.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%205%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20**r2_score**%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20ls_model%2C%20np%2C%20r2_score%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20ls_model.predict(X_test)%0A%0A%20%20%20%20np.round(r2_score(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%206%0A%0A%20%20%20%20How%20many%20coefficients%20have%20a%20value%20in%20the%20range%20%5B-1%2C%201%5D%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ls_model)%3A%0A%20%20%20%20_coef%20%3D%20ls_model.coef_%0A%0A%20%20%20%20len(_coef%5B(_coef%20%3E%3D%20-1)%20%26%20(_coef%20%3C%3D%201)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%207%0A%0A%20%20%20%20Train%20a%20KNeighborsRegressor%20with%20the%20following%20parameters%3A%0A%0A%20%20%20%20-%20n_neighbors%20%3D%2010%0A%20%20%20%20-%20p%20%3D%201%0A%0A%20%20%20%20Predict%20the%20target%20for%20the%20test%20data%20and%20compute%20the%20root_mean_squared_error(RMSE)%20on%20the%20same%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(KNeighborsRegressor)%3A%0A%20%20%20%20knr_model%20%3D%20KNeighborsRegressor(n_neighbors%3D10%2C%20p%3D1)%0A%20%20%20%20return%20(knr_model%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20knr_model%2C%20y_train)%3A%0A%20%20%20%20knr_model.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%207%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20**rmse**%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20knr_model%2C%20np%2C%20root_mean_squared_error%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20knr_model.predict(X_test)%0A%0A%20%20%20%20np.round(root_mean_squared_error(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%208%0A%0A%20%20%20%20Train%20a%20Decision%20Tree%20Regressor%20with%20the%20following%20parameters%3A%0A%0A%20%20%20%20-%20max_depth%20%3D%2010%0A%20%20%20%20-%20min_samples_split%20%3D%206%0A%20%20%20%20-%20min_samples_leaf%20%3D%206%0A%20%20%20%20-%20random_state%20%3D%2042%0A%0A%20%20%20%20Predict%20the%20target%20for%20the%20test%20data%20and%20compute%20the%20%60root_mean_squared_error%60(RMSE)%20on%20the%20same%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DecisionTreeRegressor)%3A%0A%20%20%20%20dtr_model%20%3D%20DecisionTreeRegressor(%0A%20%20%20%20%20%20%20%20max_depth%3D10%2C%20min_samples_split%3D6%2C%20min_samples_leaf%3D6%2C%20random_state%3D42%0A%20%20%20%20)%0A%20%20%20%20return%20(dtr_model%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20dtr_model%2C%20y_train)%3A%0A%20%20%20%20dtr_model.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%208%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20**rmse**%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20dtr_model%2C%20np%2C%20root_mean_squared_error%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20dtr_model.predict(X_test)%0A%0A%20%20%20%20np.round(root_mean_squared_error(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Question%209%20-%2010%0A%0A%20%20%20%20Perform%20Hyperparameter%20Tuning%20using%20GridSearchCV%20on%20AdaBoostRegressor.%20Use%20%60random_state%20%3D%2042%60.%20Value%20of%20%60cv%20%3D%204%60.%20Hyperparameter%20tuning%20is%20to%20be%20done%20over%3A%0A%0A%20%20%20%20-%20n_estimators%20%3D%20%5B10%2C50%2C100%2C200%2C500%5D%0A%20%20%20%20-%20learning_rate%20%3D%20%5B0.1%2C0.5%2C1%2C2%5D%0A%0A%20%20%20%20Use%20the%20best%20model%20obtained%20to%20compute%20the%20score%20on%20the%20test%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20params_grid%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22n_estimators%22%3A%20%5B10%2C%2050%2C%20100%2C%20200%2C%20500%5D%2C%0A%20%20%20%20%20%20%20%20%22learning_rate%22%3A%20%5B0.1%2C%200.5%2C%201%2C%202%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(params_grid%2C)%0A%0A%0A%40app.cell%0Adef%20_(AdaBoostRegressor)%3A%0A%20%20%20%20adaboost%20%3D%20AdaBoostRegressor(random_state%3D42)%0A%20%20%20%20return%20(adaboost%2C)%0A%0A%0A%40app.cell%0Adef%20_(GridSearchCV%2C%20adaboost%2C%20params_grid)%3A%0A%20%20%20%20grid_search%20%3D%20GridSearchCV(%0A%20%20%20%20%20%20%20%20estimator%3Dadaboost%2C%20param_grid%3Dparams_grid%2C%20cv%3D4%2C%20n_jobs%3D-1%2C%20scoring%3D%22r2%22%0A%20%20%20%20)%0A%20%20%20%20return%20(grid_search%2C)%0A%0A%0A%40app.cell%0Adef%20_(X_train%2C%20grid_search%2C%20y_train)%3A%0A%20%20%20%20grid_search.fit(X_train%2C%20y_train)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%209%0A%0A%20%20%20%20What%20is%20the%20value%20of%20the%20r2_score%20obtained%20on%20the%20test%20dataset%3F%20%5BEnter%203%20decimal%20places%5D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(X_test%2C%20grid_search%2C%20np%2C%20r2_score%2C%20y_test)%3A%0A%20%20%20%20_y_pred%20%3D%20grid_search.predict(X_test)%0A%0A%20%20%20%20np.round(r2_score(y_test%2C%20_y_pred)%2C%203)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Question%2010%0A%0A%20%20%20%20What%20is%20the%20value%20of%20%60n_estimators%60%20obtained%20for%20the%20best%20model%20after%20training%20with%20**GridSearchCV**%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(grid_search)%3A%0A%20%20%20%20grid_search.best_params_%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
3483ffd5fe0255f6d835e9231d17b84d02e534419a10a9e8eaa4033b64e76136